
#include "net.h"

#include "log.h"

int get_socket()
{
#if defined(LINUX)
	int fd;
	fd = socket(AF_INET, SOCK_STREAM, 0);
	return fd;
#elif defined(WIN32)
	WORD wVersionRequested;
	WSADATA wsaData;
	int err;
	int fd;
	int error;
	wVersionRequested = MAKEWORD( 2, 2 );
	err = WSAStartup( wVersionRequested, &wsaData );
	if ( err != 0 ) {
		/* Tell the user that we could not find a usable */
		/* WinSock DLL.                                  */
		return -1;
	}
	
	
	fd = socket(AF_INET, SOCK_STREAM, 0);
	if (fd == -1) {
		error = WSAGetLastError() ;
		switch(error)
		{
		case WSANOTINITIALISED:
			LOG_DEBUG("A successful WSAStartup must occur before using this function.\n");
			break;
		case WSAENETDOWN:
			LOG_DEBUG("The network subsystem or the associated service provider has failed.\n");
			break;
		case WSAEAFNOSUPPORT:
			LOG_DEBUG("The specified address family is not supported.");
			break;
		case WSAEINPROGRESS:
		case WSAEMFILE:
		case WSAENOBUFS:
		case WSAEPROTONOSUPPORT:
		case WSAEPROTOTYPE:
		case WSAESOCKTNOSUPPORT:
		default:
			break;
		}
	}
	return fd;
#else
# error "platform not specified"
#endif
}

void close_socket(int fd)
{
#ifdef LINUX
	close(fd);
#elif defined (WIN32)
	closesocket(fd);
#else 
#	error "platform unknown"
#endif
}